/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import entities.User;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import other.Costants;

/**
 *
 * @author alessandro
 */
public class UserDAO {
    /*@PersistenceUnit(unitName="HeartsSDITeamPU")
    static EntityManagerFactory emf;
    @PersistenceContext(unitName="HeartsSDITeamPU")
    static EntityManager em;*/

    public static boolean update(User user) {
        boolean flag = false;
        User userT;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(Costants.PERSISTENCE_UNIT);
            EntityManager em = emf.createEntityManager();
            em.getTransaction().begin();
            userT = em.find(User.class, user.getUser());
            userT.setPunti(user.getPunti());
            em.persist(userT);
            em.getTransaction().commit();
            em.close();
            emf.close();
            flag = true;
        } catch (Exception e) {
            System.out.print(e.toString());
            System.out.println(e.getMessage());
        }

        return flag;
    }

        public static boolean newUser(User user) {
        boolean flag = false;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(Costants.PERSISTENCE_UNIT);
            EntityManager em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(user);
            em.getTransaction().commit();
            em.close();
            emf.close();
            flag = true;
        } catch (Exception e) {
            System.out.println("Utente non creato");
            System.out.print(e.toString());
            System.out.println(e.getMessage());
        }

        return flag;
    }

    public static boolean updateList(ArrayList<User> userList) {
        boolean flag = false;
        if (userList != null && !userList.isEmpty()) {
            try {
                EntityManagerFactory emf = Persistence.createEntityManagerFactory(Costants.PERSISTENCE_UNIT);
                EntityManager em = emf.createEntityManager();
                em.getTransaction().begin();
                for (int i = 0; i < userList.size(); i++) {
                    User userT;
                    userT = em.find(User.class, userList.get(i).getUser());
                    userT.setPunti(userList.get(i).getPunti());
                    em.persist(userT);
                    em.getTransaction().commit();
                }
                flag = true;
                em.close();
                emf.close();
            } catch (Exception e) {
                System.out.print(e.toString());
                System.out.println(e.getMessage());
            }
        }
        return flag;
    }

    public static User findUser(String username, String password) {
        User user = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(Costants.PERSISTENCE_UNIT);
            EntityManager em = emf.createEntityManager();
            user = (User) em.createNamedQuery("User.findByUserAndPassword").setParameter("user", username).setParameter("password", password).getResultList().get(0);
            em.close();
            emf.close();
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.print(e.toString());
            System.out.println(e.getMessage());
        }
        return user;

    }

    public static User findUserByUsername(String username) {
        User user = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(Costants.PERSISTENCE_UNIT);
            EntityManager em = emf.createEntityManager();
            user = (User) em.createNamedQuery("User.findByUser").setParameter("user", username).getResultList().get(0);
            em.close();
            emf.close();
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.print(e.toString());
            System.out.println(e.getMessage());
        }
        return user;
    }

    public static List<User> getRank() {
        List<User> rank = new ArrayList<User>();
        try {
           EntityManagerFactory emf = Persistence.createEntityManagerFactory(Costants.PERSISTENCE_UNIT);
            EntityManager em = emf.createEntityManager();
            rank = em.createNamedQuery("User.findAll").getResultList();
            em.close();
            emf.close();
        } catch(Exception e) {
            System.out.print(e.toString());
            System.out.println(e.getMessage());
        }
        return rank;

    }
}
